package mis510.homefinders;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Rm_login_req extends Activity{
	Button btnLogin;
	Button btnPW;
	Button bk_btn;
	EditText username;
	EditText password;
	Intent nextActivity;
	TextView registerScreen;
	DBUserAdapter dbUser = new DBUserAdapter(Rm_login_req.this);
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rm_login_req);
        System.out.println("rm_login_req");
	
	registerScreen = (TextView) findViewById(R.id.link_to_register);
	btnLogin = (Button) findViewById(R.id.btnLogin);
	btnPW = (Button) findViewById(R.id.btnPW);
	bk_btn = (Button) findViewById(R.id.bk_btn);
	username = (EditText) findViewById(R.id.username);
	password = (EditText) findViewById(R.id.password);
	dbUser.open();
	 
    // Listening to register new account link
    registerScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Switching to Register screen
            String url = "http://ec2-54-245-183-169.us-west-2.compute.amazonaws.com:8080/HomeFinders/index.html";  //change to our website
            Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
            startActivity(i);
        }
    });
    
	
    btnLogin.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        	String username_login = username.getText().toString();
        	String password_login = password.getText().toString();
        	System.out.println("username and pw: "+username_login+password_login);
        	if (String.valueOf(username_login).trim().length() >0 || String.valueOf(password_login).trim().length()>0 )
        	{
        		//nextActivity = new Intent(SearchProperty.this, Evaluation.class);
        		//startActivity(nextActivity);
        		//nextActivity.putExtra("address", address);
        		//startActivityForResult(nextActivity, 1);
        		DBUserAdapter dbUser = new DBUserAdapter(Rm_login_req.this);
				dbUser.open();

				if(dbUser.Login(username_login, password_login))
				{
					Toast.makeText(Rm_login_req.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
					String email = dbUser.Emailretrieve(username_login);
					System.out.println("email: "+email);
					dbUser.close();
					//setting user status
//					SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
//					SharedPreferences.Editor editor = settings.edit();
//					editor.putString("username", username_login);
					if(email!=null)
					{
						nextActivity = new Intent(Rm_login_req.this, Roommatereq.class);
						nextActivity.putExtra("useremail", email);
            			startActivityForResult(nextActivity, 1);
					}
				}
				else{
					
					Toast.makeText(Rm_login_req.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();
				}
				dbUser.close();
        		
        	}
        	else 
        	{
        		Toast toast_login = Toast.makeText(getApplicationContext() , "Please fill out both user name and password", Toast.LENGTH_SHORT);
        		toast_login.show();
        	}
        }
    });
    
    btnPW.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
        	nextActivity = new Intent(Rm_login_req.this, GetPW.class);
        	startActivity(nextActivity);
        }
    
    
	});
    
    bk_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        	nextActivity = new Intent(Rm_login_req.this, SearchProperty.class);
        	startActivity(nextActivity);
        }
    });
	}

}
